package com.qf.boot2302.controller;

import com.qf.boot2302.entity.Student;
import com.qf.boot2302.service.StudentService;
import com.qf.boot2302.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/stu")
@Slf4j
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/query")
    public ResultVo querybypage(@RequestParam("pagenum") Integer pagenum,@RequestParam("pagesize") Integer pagesize){

        log.info("入参为:pagenum={},pagesize={}",pagenum,pagesize);
        if (pagenum<1 || pagesize<0 || pagesize>20){
            return ResultVo.error(-1,"分页参数非法");
        }

//        List<Student> data = studentService.querybypage(pagenum,pagesize);
        HashMap<String, Object> data = studentService.querybypageInfo(pagenum,pagesize);

        log.info("查询到分页数据为:data={}",data);
        return ResultVo.ok(1, "ok", data);

    }


    @RequestMapping("fairpick/db")
    public ResultVo pickName() throws Exception {
        log.info("开始进行基于数据库的随机点名");
        Student student = studentService.findPickOneStudent();
        log.info("点中的学生为,student={}",student);
        if (student!=null){
            return ResultVo.ok(1, "ok", student.getName());
        }else{
            return ResultVo.error(-1, "都点完了");
        }
    }

    @RequestMapping("/reset")
    public ResultVo reset(){
        log.info("重置点名状态");
        studentService.reset();
        log.info("重置点名成功");
        return ResultVo.ok(1, "重置成功了", null);
    }
}
